import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Action, Store } from '@ngrx/store';
import { Actions, Effect } from '@ngrx/effects';
import {
    TaskListActionTypes,
    TaskListLoadSuccessAction,
    TaskListLoadFailAction,
    TaskListAddSuccessAction,
    TaskListAddFailAction,
    TaskListUpdateSuccessAction,
    TaskListUpdateFailAction,
    TaskListDeleteSuccessAction,
    TaskListDeleteFailAction,
    TaskListSwapSuccessAction,
    TaskListSwapFailAction
} from '../actions/task-list.action';
import { toPayload } from '@ngrx/effects/src/util';
import * as fromRoot from '../reducers';
import { Router } from '@angular/router';
import { TaskListService } from '../services/task-list.service';
import { TaskLoadAction } from '../actions/task.action';
@Injectable()
export class TaskListEffects {
    @Effect()
    loadTaskLists$: Observable<Action> = this.actions$
        .ofType(TaskListActionTypes.LOAD)
        .map(toPayload)
        .switchMap((projectId) => this.service$.get(projectId)
            .map(taskLists => new TaskListLoadSuccessAction(taskLists))
            .catch(err => Observable.of(new TaskListLoadFailAction(JSON.stringify(err)))));
    @Effect()
    addTaskLists$: Observable<Action> = this.actions$
        .ofType(TaskListActionTypes.ADD)
        .map(toPayload)
        .switchMap((taskList) => this.service$.add(taskList)
            .map(tl => new TaskListAddSuccessAction(tl))
            .catch(err => Observable.of(new TaskListAddFailAction(JSON.stringify(err))))
        );

    @Effect()
    updateTaskLists$: Observable<Action> = this.actions$
        .ofType(TaskListActionTypes.UPDATE)
        .map(toPayload)
        .switchMap((taskList) => this.service$.update(taskList)
            .map(tl => new TaskListUpdateSuccessAction(tl))
            .catch(err => Observable.of(new TaskListUpdateFailAction(JSON.stringify(err)))));
    @Effect()
    delTaskLists$: Observable<Action> = this.actions$
        .ofType(TaskListActionTypes.DELETE)
        .map(toPayload)
        .switchMap((taskList) => this.service$.del(taskList)
            .map(tl => new TaskListDeleteSuccessAction(tl))
            .catch(err => Observable.of(new TaskListDeleteFailAction(JSON.stringify(err)))));

    @Effect()
    swapTaskLists$: Observable<Action> = this.actions$
        .ofType(TaskListActionTypes.SWAP)
        .map(toPayload)
        .switchMap(({ src, target }) => this.service$.swapOrder(src, target)
            .map(tasklists => new TaskListSwapSuccessAction(tasklists))
            .catch(err => Observable.of(new TaskListSwapFailAction(JSON.stringify(err)))));

    @Effect()
    loadTaskByList$: Observable<Action> = this.actions$
        .ofType(TaskListActionTypes.LOAD_SUCCESS)
        .map(toPayload)
        .map(lists => new TaskLoadAction(lists));
    constructor(
        private actions$: Actions,
        private service$: TaskListService,
        private store$: Store<fromRoot.State>,
        private router: Router
    ) { }
}
